package com.jxd.ordering.service.impl;

import com.jxd.ordering.dao.AdminMapper;
import com.jxd.ordering.dao.UserMapper;
import com.jxd.ordering.model.Admin;
import com.jxd.ordering.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

/**
 * @ClassName AdminServiceImpl
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/28
 * @Version 1.0
 */
@Service
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增管理员信息
     * @param admin 管理员实体类
     * @return 是否新增成功
     */
    @Override
    public boolean addAdmin(Admin admin) {
        return adminMapper.insertAdmin(admin);
    }

    /**
     * 修改管理员信息
     * @param admin 管理员实体类
     * @return 是否修改成功
     */
    @Override
    public boolean editAdmin(Admin admin) {
        return adminMapper.updateAdmin(admin);
    }

    /**
     * 批量删除管理员信息
     * @param adminnos 管理员编号数组
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public boolean delAdmin(int[] adminnos) {
        //开启事务
        int[] userids = new int[adminnos.length];
        for (int i = 0; i < adminnos.length; i++) {
            userids[i] = adminMapper.selectAdminByAdminno(adminnos[i]).getUserid();
        }

        adminMapper.deleteAdmin(adminnos);
        userMapper.deleteUser(userids);
        return true;
    }

    /**
     * 按管理员编号查询管理员信息
     * @param adminno 管理员编号
     * @return 查询结果
     */
    @Override
    public Admin getAdminByAdminno(int adminno) {
        return adminMapper.selectAdminByAdminno(adminno);
    }
}
